package cn.pegasus.teamwork.pegasus.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFundstatistics;
import cn.pegasus.teamwork.pegasus.entity.BussWasherInfo;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussEquipmentInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussWasherFunddetailsService;
import cn.pegasus.teamwork.pegasus.service.BussWasherFundstatisticsService;
import cn.pegasus.teamwork.pegasus.service.BussWasherInfoService;
import cn.pegasus.teamwork.util.HSSFWorkUtil;
import cn.pegasus.teamwork.vo.WasherOrderVo;

/**
 * 洗车工账号
 * @author 40854
 *
 */
@Controller
@RequestMapping("/washerInfo")
public class WasherInfoController extends BaseManagerController<BussWasherInfo, BussWasherInfoMapper>{
	
	private static final Logger logger = LoggerFactory.getLogger(WasherInfoController.class);
	
	@Autowired
	private BussWasherInfoService washerInfoService;
	@Autowired
	private BussWasherFundstatisticsService washerFundstatisticsService;
	
	@Autowired
	private BussWasherFunddetailsService funddetailsService;
	
	@Autowired
	private BussEquipmentInfoService equipmentInfoService;
	@Autowired
	private BussUcenterAreaService ucenterAreaService;
	/**
	 * 分页查询洗车工信息列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("user/washer-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussWasherInfo> dicList = washerInfoService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询洗车工信息列表异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 状态操作
	 */
	@ResponseBody
	@RequestMapping(value="/updateS")
	public String update(
			@RequestParam(value="id",required = false) Integer id,
			@RequestParam(value="washerStatus",required = false) Integer washerStatus,
			@RequestParam(value="washerIsdepEntrust",required = false) Integer washerIsdepEntrust,
			@RequestParam(value="washerExamine",required = false) Integer washerExamine){
		try {
			washerInfoService.updateStatus(id, washerStatus,washerIsdepEntrust,washerExamine,null);
        } catch (Exception e) {
            logger.error("操作洗车工状态异常", e);
            return "1001";
        }
		return "200";
	}
	
	/**
	 * 查看详情
	 */
	 @RequestMapping(value="/findWasherInfo")
	 public ModelAndView findWasherInfo(@RequestParam(value="id",required = false) Integer id){
		 ModelAndView view = new ModelAndView("user/washer-info");
		 BussWasherInfo info = washerInfoService.findWasherInfo(id);
		 view.addObject("washerInfo", info);
		 return view;
	 }
	 
	 /**
	  * 跳转修改页面
	  * @param id
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping(value="/updateAp")
	 public ModelAndView updateAp(
			 @RequestParam(value="id",required = false) Integer id) throws Exception{
		 ModelAndView view = new ModelAndView("user/washer-update");
		 BussWasherInfo info = washerInfoService.findWasherInfo(id);
		 view.addObject("info", info);
		 List<BussUcenterArea> areaList = ucenterAreaService.findList(null, 4);
		 view.addObject("areaList", areaList);
		 return view;
	 }
	 
	 /**
	  * 修改洗车工信息
	  */
	 @ResponseBody
	 @RequestMapping(value = "/update")
	 public Map<String, Object> update(@RequestParam Map<String, Object> map){
		Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
        	washerInfoService.update(map);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("修改洗车工信息异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
        return resultMap;
	 }
	 
	 
	 /**
	  * 跳转添加机器码页面
	 * @throws Exception 
	  */
	 @RequestMapping(value="/addMachinecodeAp")
	 public ModelAndView addMachinecodeAp(
			 @RequestParam(value="id",required = false) Integer id) throws Exception{
		 ModelAndView view = new ModelAndView("user/washer-code");
		 List<BussUcenterArea> areaList = ucenterAreaService.findList(null, 4);
		 BussWasherInfo info = washerInfoService.findWasherInfo(id);
		 view.addObject("info", info);
		 view.addObject("areaList", areaList);
		 return view;
	 }
	 
	 /**
	  * 添加机器码
	  */
	 @ResponseBody
		@RequestMapping(value="/addMachinecode")
		public Map<String,Object> addMachinecode(
				@RequestParam Map<String, Object> map){
			Map<String, Object> resultMap = new HashMap<String, Object>();
			try {
				washerInfoService.updateWasher(map);
	            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
	        } catch (Exception e) {
	            logger.error("编辑洗车工信息异常异常", e);
	            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
	        }
			return resultMap;
		}
	 
	 /**
	  * 洗车工账号列表
	  */
	 @RequestMapping(value="/listAccount")
		public ModelAndView listAccount(@RequestParam Map<String, Object> map){
			ModelAndView view = new ModelAndView("user/washer-account");
			if(map.isEmpty()){
				map.put("pageData", 10);
				map.put("curPage", 1);
			}
			try{
				PageObject<BussWasherFundstatistics> dicList = washerFundstatisticsService.PageList(map);
				view.addObject("list", dicList);
				view.addObject("map", map);
			}catch(Exception e){
				logger.error("查询区域列表信息异常",e.getMessage());
			}
			return view;
		}
	 
	 /**
	  * 洗车工资金明细
	  */
	 @RequestMapping(value="/funddetailsList")
	public ModelAndView funddetailsList(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("finance/finddetail-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussWasherFunddetails> dicList = funddetailsService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询区域列表信息异常",e.getMessage());
		}
		return view;
	}
	 /**
	  * 洗车工审核
	  * @param map
	  * @return
	  */
	 @RequestMapping(value="/washerAud")
	 public ModelAndView washerAud(){
			ModelAndView view = new ModelAndView("finance/washerAud-list");
			Map<String,Object> map = new HashMap<String,Object>();
			if(map.isEmpty()){
				map.put("pageData", 10);
				map.put("curPage", 1);
			}
			try{
				PageObject<BussWasherFunddetails> dicList = funddetailsService.PageWasher(map);
				view.addObject("list", dicList);
				view.addObject("map", map);
			}catch(Exception e){
				logger.error("查询区域列表信息异常",e.getMessage());
			}
			return view;
		}
	 
	 /**
	  * 退款申请
	  */
	 @ResponseBody
	@RequestMapping(value="/updateStatus")
	public Map<String,Object> updateStatus(
			@RequestParam(value="id",required = false) Long id,
			@RequestParam(value="ispaymoney",required = false) Integer ispaymoney,
			@RequestParam(value="examineStatus",required = false) Integer examineStatus){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			funddetailsService.updateStatus(id, ispaymoney,examineStatus);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("操作洗车工状态异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
		return resultMap;
	}
	 
	/**
	 * 退押金
	 */
	 @ResponseBody
	@RequestMapping(value="/updateDeposit")
	public Map<String,Object> updateDeposit(
			@RequestParam(value="id",required = false) Integer id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			funddetailsService.updateDeposit(id);
            resultMap = ReMessage.resultBack(ParameterEunm.SUCCESSFUL_CODE, null);
        } catch (Exception e) {
            logger.error("操作洗车工状态异常", e);
            resultMap = ReMessage.resultBack(ParameterEunm.ERROR_500_CODE, null);
        }
		return resultMap;
	}
	
	 /**
	  * 下载
	  */
	 @RequestMapping(value = "/downReward")
	 public void downReward(HttpServletRequest request,HttpServletResponse response,Long id){
		 try {
			String fileName = "washer.xlsx";
			if(request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") > 0){// FF
			    fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");  
			}else{// Other
				fileName = fileName;  
			}
			response.setContentType("application/octet-stream; charset=utf-8");     
			response.setHeader("Content-disposition", "attachment;filename=" + fileName);     
			OutputStream out = response.getOutputStream();
			
			 Map<String,Object> beans=new HashMap<String,Object>();
		    /**
		     * 数据
		     */
			 List<WasherOrderVo> list = funddetailsService.findWasherOrderVo(id);
		    String templateFile="washer.xlsx";
		    beans.put("list", list);
			//调用引擎生成excel报表
		    HSSFWorkUtil.exportExcelJxl(templateFile, beans, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error("导出excel文件异常", e);
		} 
	 }
}
